package com.wyvoid.forum.web.action.controlPanel;

import com.wyvoid.forum.web.action.BaseAction;
import com.wyvoid.forum.annotations.LoginUser;
import com.wyvoid.forum.domain.User;
import com.wyvoid.forum.domain.UserProfile;
import com.wyvoid.forum.service.IUserService;
import net.sourceforge.stripes.action.UrlBinding;
import net.sourceforge.stripes.action.DontValidate;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.integration.spring.SpringBean;
import net.sourceforge.stripes.validation.ValidateNestedProperties;
import net.sourceforge.stripes.validation.Validate;

/**
 * Created by IntelliJ IDEA.
 * User: wangchao
 * Date: 2007-7-1
 * Time: 14:47:55
 * To change this template use File | Settings | File Templates.
 */
@UrlBinding("/controlPanel/editUserData.do")
@LoginUser
public class EditUserDataAction extends BaseAction {
    @SpringBean
    private IUserService userService;
     @ValidateNestedProperties({
    @Validate(field = "nickname", required = true, maxlength = 50),
    @Validate(field = "email", required = true, maxlength = 50)
            })
    private User oldUser;
    private UserProfile userProfile;


    public void setUserService(IUserService userService) {
        this.userService = userService;
    }

    public User getOldUser() {
        return oldUser;
    }

    public void setOldUser(User oldUser) {
        this.oldUser = oldUser;
    }

    public UserProfile getUserProfile() {
        return userProfile;
    }

    public void setUserProfile(UserProfile userProfile) {
        this.userProfile = userProfile;
    }

    @DontValidate
     @DefaultHandler
    public Resolution editUserDataForm() {
        oldUser = userService.getUser(getContext().getUserId());
        userProfile = userService.getUserProfile(getContext().getUserId());
        return getBBSResolution("controlPanel/editUserData");

    }


    public Resolution editUserData() {
        User updateUser = userService.getUser(oldUser.getId());
        UserProfile updateUserProfile = userService.getUserProfileById(oldUser.getId());
        updateUser.setNickname(oldUser.getNickname());
        updateUser.setEmail(oldUser.getEmail());
        updateUser.setSex(oldUser.getSex());
        updateUser.setPhotoUrl(oldUser.getPhotoUrl());
        //userProfile
        updateUserProfile.setBirthDay(userProfile.getBirthDay());
        updateUserProfile.setWebsite(userProfile.getWebsite());
        updateUserProfile.setImQq(userProfile.getImQq());
        updateUserProfile.setImMsn(userProfile.getImMsn());
        updateUserProfile.setAddress(userProfile.getAddress());
        updateUserProfile.setIntroduce(userProfile.getIntroduce());
        userService.updateUserAndProfile(updateUser, updateUserProfile);
        return getBBSResolution("message/success");
    }
}
